package com.jandan.ui.client;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.jandan.ui.client.service.WordLibService;
import com.jandan.ui.client.service.WordLibServiceAsync;
import com.jandan.ui.model.CnAndSentence;
import com.jandan.ui.model.LearnedWord;
import com.jandan.ui.model.Word;

public class WordWindow extends Window {
	private Word word;

	public WordWindow() {

	}

	public WordWindow(Word word) {
		this.word = word;
		createComponent();
	}

	public WordWindow(LearnedWord learnedWord) {
		this.word = learnedWord.getWord();
		createComponent();
	}

	public void createComponent() {
		this.setTitle(word.getWordEn());
		this.setWidth(409);
		this.setHeight(313);
		this.setResizable(false);
		this.setLayout(new FitLayout());
		this.setPaddings(5);
		this.setClosable(true);
		this.setCloseAction(Window.CLOSE);
		this.setPlain(true);

		Panel contentPanel = new Panel();
		contentPanel.setLayout(new VerticalLayout(20));
		this.setPaddings(10);
		contentPanel.setFrame(false);
		contentPanel.setBorder(false);
		contentPanel.setHtml(this.getContent());
		
		Hyperlink link = new Hyperlink();
		link.setText("添加到生词本");
		link.addStyleName("status-panel");
		contentPanel.add(link);
		
		link.addClickListener(new ClickListener(){

			public void onClick(Widget arg0) {
				// TODO Auto-generated method stub
				WordLibServiceAsync wordLibService = WordLibService.Util.getInstance();
				wordLibService.addStrangeWord(word,new AsyncCallback<Boolean>(){

					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						MessageBox.alert("添加生词错误");
					}

					public void onSuccess(Boolean result) {
						// TODO Auto-generated method stub
						boolean r = result.booleanValue();
						if(r){
							MessageBox.alert("生词" + word.getWordEn() + "添加成功");
						}
						else {
							MessageBox.alert(word.getWordEn() + "已在生词本中");
						}
					}		
				});

			}
			
		});

		this.add(contentPanel);
	}

	public String getContent() {
		StringBuffer sb = new StringBuffer("<p><h3>");
		sb.append(word.getWordEn());
		sb.append("</h3><br/>");
		sb.append("<span class='phonetics'>");
		sb.append(word.getPhonetics());
		sb.append("</span>");
		
		List<CnAndSentence> cnAndSentenceList = word.getCnAndSentenceList();
		if(cnAndSentenceList != null){
			CnAndSentence cs = null;
			String c = null;
			String s = null;
			for(int i = 0,n = cnAndSentenceList.size();i < n;i++){
				cs = cnAndSentenceList.get(i);
				c = cs.getCn();
				s = cs.getSentence();
				
				sb.append("<div><span>");
				sb.append(c);
				sb.append("</span><br/>");
				sb.append("<span>");
				sb.append("</span></div>");
				sb.append("<br/>");

			}
		}

		return sb.toString();
	}
}
